<?php

class ProductController extends FrontendController
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}
	public function actionView($id)
	{
		$subcategory = SubCategory::model()->findByAttributes(array('SubCateNameRewrite' => $id));
		//$product = Product::model()->findAllByAttributes(array('SubCategoryId' => $id));
		$criteria = new CDbCriteria();
		$count = Product::model()->count($criteria);
		$pages=new CPagination($count);

	    // results per page
	    $pages->pageSize = 9;
	    $pages->applyLimit($criteria);
	    $criteria->condition = 't.Status="1" and t.SubCategoryId="' . $subcategory->SubCateId .'"';
	    $criteria->order = 't.Price ASC';
	    $product = Product::model()->findAll($criteria);
	    $page = false;
	    if($count > $pages->pageSize){
	    	$page = true;
	    }
		$this->render('view',array(
			'product'=> $product,
			'sub' => $subcategory,
			'pages' => $pages,
			'page'=>$page,
		));
	}

	public function actionIndex()
	{
		$product = new Product();
		$criteria = new CDbCriteria();
        $criteria->select = 't.*';
        $criteria->with= 'category';
        //$criteria->with= 'subcategory';
        //$criteria->with= 'brand';
        //$criteria->with= 'genre';
        $criteria->join= 'LEFT JOIN Categories c ON t.CategoryId = c.CategoryId';
        $criteria->join= 'LEFT JOIN SubCategories s ON t.SubCategoryId = s.SubCateId';
        $criteria->join= 'LEFT JOIN Brands b ON t.BrANDId = b.BrANDId';
        $criteria->join= 'LEFT JOIN Genres g ON t.GenreId = g.GenreId';
        $criteria->limit = '3';
        $criteria->order = 't.CreateDate DESC';
        $criteria->condition = 't.Status ="1" AND category.Status = "1"';// AND subcategory.Status= :pStatus';// AND brand.Status=:pStatus AND genre.Status=:pStatus';

        $data = $product->findAll($criteria);
		$this->render('index',array(
			'data' => $data));
	}
	public function loadModel($id)
	{
		$model=Product::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	public function actionSearch_product(){
		// $subcate = SubCategory::model()->findByAttributes(array('SubCateNameRewrite' => 'giay-bup-be'));
			
		// 	var_dump($subcate->SubCateId);
		// 	die;
		if($_POST['subid']){
			$subcate = SubCategory::model()->findByAttributes(array('SubCateNameRewrite' => $_POST['subid']));
			$subcateid = $subcate->SubCateId;
			$subsub = $_POST['id']!= '' ? ' and t.SubSubCateId in('. $_POST['id'] .')' : '';
			$color = $_POST['color'] != '' ? ' and t.ColorId in (' .$_POST['color'] . ')' : '';
			$size = $_POST['size'] != '' ? ' and t.SizeId in (' . $_POST['size'] . ')' : '';
			$brand = $_POST['brand'] != '' ? ' and t.BrandId in (' .$_POST['brand'] .')' :''; 
			//$price = ' and t.Price in (' .str_replace(".", "", $_POST['minprice']) .','.str_replace(".", "", $_POST['maxprice']) .')';
			$condition = ' and t.SubCategoryId ="' . $subcateid. '" ' . $subsub . $color . $size . $brand; 
			$criteria = new CDbCriteria();
			$criteria->select = 't.*';
			//$criteria->with = 'subcategory';
			//$criteria->join = 'JOIN SubCategories s on s.SubCateId = t.SubCategoryId';
			$criteria->condition = 't.Status="1"' . $condition;
			$criteria->order = 't.Price ' . $_POST['orderDisplay'];
			$criteria->limit = $_POST['showProduct'];
			$count = Product::model()->count($criteria);
			$pages=new CPagination($count);
		    // results per page
		    $pages->pageSize=3;
		    $pages->applyLimit($criteria);
			$product = Product::model()->findAll($criteria);
			echo CJSON::encode(array($product, $pages));	
		}

	}
	
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='product-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
